package com.zqf.web.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;



@Controller
public class SimpMessageService {
	Logger log = LoggerFactory.getLogger(SimpMessageService.class);
	@Autowired SimpMessageSendingOperations messagingTemplate;
	
	 @PostMapping("/api/topic/send")
	 public ResponseEntity<String> send(
			 @RequestParam String destination,
			 @RequestBody   Object payload) {
		 log.debug("sendObject destination {} -> {} ",destination, payload);	
		 messagingTemplate.convertAndSend(destination, payload);
		 return ResponseEntity.ok("send ok");
	 }
	 
	 @PostMapping("/api/topic/sendString")
	 public ResponseEntity<String> sendString(
			 @RequestParam String destination,
			 @RequestParam String payload) {
		 log.debug("sendString destination {} -> {} ",destination, payload);	
		 messagingTemplate.convertAndSend(destination, payload);
		 return ResponseEntity.ok("send ok");
	 }
}
